import "./basic_entity.dart";

class DiaryTable {
  static const String tableName = 'diary';
  static const String id = 'id';
  static const String title = 'title';
  static const String image = 'image';
  static const String content = 'content';
  static const String date = 'date';

  static const createTableSql = '''
create table $tableName (
  $id integer primary key autoincrement,
  $title text,
  $image text,
  $content text,
  $date text),
''';
}

class DiaryEntity extends BasicEntity {
  late String title;
  late String image;
  late String content;
  late DateTime date;

  DiaryEntity();

  @override
  DiaryEntity fromMap(Map<String, Object?> map) {
    id = map[DiaryTable.id] as int;
    title = map[DiaryTable.title] as String;
    image = map[DiaryTable.image] as String;
    content = map[DiaryTable.content] as String;
    date = DateTime.fromMillisecondsSinceEpoch(
      int.parse(map[DiaryTable.date] as String),
    );
    return this;
  }

  @override
  Map<String, Object?> toMap() {
    var map = <String, Object?>{
      DiaryTable.title: title,
      DiaryTable.image: image,
      DiaryTable.content: content,
      DiaryTable.date: date.millisecondsSinceEpoch,
    };
    return map;
  }

  @override
  String toString() {
    return '''{
  id: $id,
  title: $title,
  image: $image,
  content: ${content.substring(0, 20)}...,
  date: $date,
}''';
  }
}
